package parser;

import adventure.Adventure;
import adventure.AdventureEdit;
import adventure.commands.skills.SkillEdit;
import adventure.placeable.Item;
import adventure.vocation.Vocation;
import adventure.vocation.VocationEdit;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import player.BookEntry;
import player.Player;
import player.PlayerEdit;
import player.PlayerManager;
import server.util.Broadcaster;

/**
 *
 * @author Michael Hanns
 *
 */
public class PlayerParse extends Parser {

    public PlayerParse(Adventure a, PlayerManager players, Broadcaster out) {
        super((AdventureEdit) a);
        this.players = players;
        this.out = out;
        System.out.println("PlayerParse: PlayerManager = null?: " + (players == null));
        System.out.println("PlayerParse: Broadcaster = null?: " + (out == null));
    }

    public Player getPlayer() {
        return (Player) player;
    }

    public void readPlayerFile(File f) {
        try {
            bufferedReader = new BufferedReader(new FileReader(f));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                readLine(line);
            }
            bufferedReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String convertToXML(Player pl) {
        String finalXML = "<player id='" + pl.getID() + "'>\n"
                + "    <name>" + formatForSaving(pl.getName().replace("corpse of ", "")) + "</name>\n"
                + "    <description>" + formatForSaving(pl.getAliveDescription()) + "</description>\n"
                + "    <level>" + pl.getLevel() + "</level>\n"
                + "    <maxhealth>" + ((PlayerEdit) pl).getMaxHealth() + "</maxhealth>\n"
                + "    <health>" + ((PlayerEdit) pl).getHealth() + "</health>\n"
                + "    <special>" + ((PlayerEdit) pl).getSpecial() + "</special>\n"
                + "    <maxspecial>" + ((PlayerEdit) pl).getMaxSpecial() + "</maxspecial>\n"
                + "    <attack>" + pl.getAttack() + "</attack>\n"
                + "    <defence>" + pl.getDefence() + "</defence>\n"
                + "    <intelligence>" + pl.getIntelligence() + "</intelligence>\n"
                + "    <speed>" + pl.getSpeed() + "</speed>\n"
                + "    <luck>" + pl.getLuck() + "</luck>\n"
                + "    <experience>" + ((PlayerEdit) pl).getXP() + "</experience>\n"
                + "    <currencyAmount>" + ((PlayerEdit) pl).getCurrencyAmount() + "</currencyAmount>\n"
                + "    <x>" + pl.x() + "</x>\n"
                + "    <y>" + pl.y() + "</y>\n"
                + "    <z>" + pl.z() + "</z>\n"
                + "    <sx>" + ((PlayerEdit) pl).sx() + "</sx>\n"
                + "    <sy>" + ((PlayerEdit) pl).sy() + "</sy>\n"
                + "    <sz>" + ((PlayerEdit) pl).sz() + "</sz>\n"
                + "    <password>" + ((PlayerEdit) pl).getPassword() + "</password>\n";
        
        if(pl.isSuperAdmin()) {
            finalXML += "    <superAdmin>" + formatForSaving(pl.isSuperAdmin()+"") + "</superAdmin>\n";
        } else if(pl.isAdmin()) {
            finalXML += "    <admin>" + formatForSaving(pl.isAdmin()+"") + "</admin>\n";
        }

        BookEntry[] e = ((PlayerEdit) pl).getAllLogEntries();
        for (int x = 0; x < e.length; x++) {
            finalXML += "    <entry id='" + e[x].getID() + "' complete='" + e[x].isComplete() + "'>\n"
                    + "        <title>" + formatForSaving(e[x].getTitle()) + "</title>\n"
                    + "        <description>" + formatForSaving(e[x].getDescription()) + "</description>\n"
                    + "        <compdesc>" + formatForSaving(e[x].getCompleteDescription()) + "</compdesc>\n"
                    + "    </entry>\n";
        }

        Item[] items = pl.getAllItems();
        for (int x = 0; x < items.length; x++) {
            if (items[x].getID() > 0 && !items[x].isTemporary()) {
                finalXML += "    <item id='" + pl.getAllItems()[x].getID() + "'";
                
                if(items[x].isConsumable()) {
                    finalXML += " usesLeft='" + items[x].getUsesLeft() + "'";
                }
                
                finalXML += ">\n";
            }
        }

        finalXML += "    <weaponeq id='" + ((PlayerEdit) pl).getWeaponID() + "'>\n";
        finalXML += "    <offhandeq id='" + ((PlayerEdit) pl).getOffhandID() + "'>\n";
        finalXML += "    <headeq id='" + ((PlayerEdit) pl).getHelmID() + "'>\n";
        finalXML += "    <chesteq id='" + ((PlayerEdit) pl).getChestID() + "'>\n";
        finalXML += "    <feeteq id='" + ((PlayerEdit) pl).getBootsID() + "'>\n";

        finalXML += "    <general>" + pl.subscribesTo("GENERAL") + "</general>\n";
        finalXML += "    <ooc>" + pl.subscribesTo("OOC") + "</ooc>\n";
        finalXML += "    <newbie>" + pl.subscribesTo("NEWBIE") + "</newbie>\n";
        finalXML += "    <trade>" + pl.subscribesTo("TRADE") + "</trade>\n";

        finalXML += "    <suppressTick>" + pl.suppressTick() + "</suppressTick>\n";

        finalXML += getVocationTag(((PlayerEdit) pl).getVocation());

        finalXML += "</player>";


        return finalXML;
    }

    private String getVocationTag(Vocation v) {
        System.out.println("Saving player vocation - " + v.getName());
        String vocationtag = "    <vocation id='" + v.getID() + "'>\n"
                + "      <name>" + formatForSaving(v.getName()) + "</name>\n"
                + "      <description>" + v.getDescription().replace("<", "{").replace(">", "}").replace("&", "&#38;") + "</description>\n"
                + "      <baseAttack>" + v.getBaseAttack() + "</baseAttack>\n"
                + "      <baseDefence>" + v.getBaseDefence() + "</baseDefence>\n"
                + "      <baseIntel>" + v.getBaseIntel() + "</baseIntel>\n"
                + "      <baseSpeed>" + v.getBaseSpeed() + "</baseSpeed>\n"
                + "      <baseLuck>" + v.getBaseLuck() + "</baseLuck>\n"
                + "      <incAttack>" + v.getChanceAttack() + "</incAttack>\n"
                + "      <incDefence>" + v.getChanceDefence() + "</incDefence>\n"
                + "      <incIntel>" + v.getChanceIntel() + "</incIntel>\n"
                + "      <incSpeed>" + v.getChanceSpeed() + "</incSpeed>\n"
                + "      <incLuck>" + v.getChanceLuck() + "</incLuck>\n";

        for (int x = 0; x < ((VocationEdit) v).getSkills().length; x++) {
            vocationtag += "      <skill id='" + ((SkillEdit) ((VocationEdit) v).getSkills()[x]).getID() + "'/>\n";
        }

        vocationtag += "    </vocation>\n";

        return vocationtag;
    }
}
